package cn.hxth.cloud.datasync.rabbitmq;

import cn.hxth.cloud.api.domain.*;
import cn.hxth.cloud.api.rabbitmq.AbstractRabbitMQHandler;
import cn.hxth.cloud.api.rabbitmq.RabbitMQSender;
import cn.hxth.cloud.service.service.InventoryService;
import cn.hxth.cloud.service.service.OrderService;
import cn.hxth.cloud.service.service.ProductionService;
import cn.hxth.cloud.service.service.WmsService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RabbitListener(queues = "data-change-queue-inventory")
public class DataChangeQueueReceiver extends AbstractRabbitMQHandler {
    @Autowired
    public DataChangeQueueReceiver(RabbitMQSender rabbitMQSender) {
        super(rabbitMQSender);
        super.setLogger(DataChangeQueueReceiver.class);
    }


    @Autowired
    private OrderService orderService;
    @Autowired
    private InventoryService inventoryService;
    @Autowired
    private ProductionService productionService;
    @Autowired
    private WmsService wmsService;
    /*@Resource
    private WmsFacadeService wmsFacadeService ;*/
    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    private  Boolean processUpdateSteelsInputInventory(JSONObject jsonObject,String jsonData){
        SteelsInputOrder steelsInputOrder = JSONObject.parseObject(jsonData,SteelsInputOrder.class) ;

        Boolean flag = this.inventoryService.updateSteelsInputInventory(steelsInputOrder);
        if(flag){
            this.redisTemplate.delete("steelsInputOrderItem");
        }
        return  flag ;
    }
    private  Boolean processUpdateSteelsOutputInventory(JSONObject jsonObject,String jsonData){
        SteelsOutputOrder steelsOutputOrder = JSONObject.parseObject(jsonData,SteelsOutputOrder.class) ;
        Boolean flag = this.inventoryService.updateSteelsOutputInventory(steelsOutputOrder);
        return  flag ;
    }



    private Boolean processUpdateSteelsInventoryForOrder(JSONObject jsonObject, String jsonData) {

        List<ProductionOrder> productionOrderList = JSONObject.parseArray(jsonData, ProductionOrder.class);
        ProductionOrder[] array = new ProductionOrder[productionOrderList.size()];
        ProductionOrder[] productionOrders = productionOrderList.toArray(array);
        return this.inventoryService.updateSteelsInventoryForOrder(productionOrders);
    }

    private Boolean processUpdateProductInventoryForOrder(JSONObject jsonObject, String jsonData) {

        Boolean flag = this.inventoryService.updateProductInventoryForOrder(JSONObject.parseObject(jsonData, OrderInfo.class));
        return flag;

    }

private  Boolean processUpdateProductOutputInventory(JSONObject jsonObject,String jsonData){
    ProductOutputOrder productOutputOrder = JSONObject.parseObject(jsonData, ProductOutputOrder.class);
    return  this.inventoryService.updateProductOutputInventory(productOutputOrder) ;
}
    private  Boolean processUpdateProductInputInventory(JSONObject jsonObject,String jsonData){
        ProductInputOrder productInputOrder = JSONObject.parseObject(jsonData, ProductInputOrder.class);
        Boolean flag = this.inventoryService.updateProductInputInventory(productInputOrder);
        if(flag){
            this.redisTemplate.delete("productInputOrderItem");
        }
        return flag ;
    }





}
